package com.read.controller;

import com.read.common.annotation.UserLoginToken;
import com.read.common.response.RespResult;
import com.read.dto.params.NovelClickParamsDto;
import com.read.dto.params.NovelUserRecommendParamsDto;
import com.read.dto.result.JwtUserDto;
import com.read.service.ClickService;
import com.read.util.RespUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author sh-sunb6
 * @description
 * @date 2019-03-09 13:25
 */
@Api(tags = "点击相关 Controller（用户点击推荐，点击小说系统推荐）")
@RestController
@RequestMapping("/click")
public class ClickController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(ClickController.class);

    @Autowired
    private ClickService clickService;

    /**
     * @description 用户点击推荐
     * @author sh-sunb6
     * @date 2019-03-09 13:51
     */
    @ApiOperation(value = "用户点击推荐", notes = "需用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "系统token", required = true, paramType = "header"),
            @ApiImplicitParam(name = "novelId", value = "小说Id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "novelName", value = "小说名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name="novelType", value="小说类型", required=true, paramType="query", dataType = "int", defaultValue = ""),
            @ApiImplicitParam(name="typeChannel", value="小说归属", required=true, paramType="query", dataType = "int", defaultValue = ""),
            @ApiImplicitParam(name="sex", value="性别(后台token获取)", required=false, paramType="query", dataType = "int", defaultValue = ""),
            @ApiImplicitParam(name="userId", value="用户id(后台token获取)", required=false, paramType="query", dataType = "int", defaultValue = "")
    })
    @PostMapping("/user")
    @UserLoginToken
    public RespResult addUserRecommend(HttpServletRequest request, NovelUserRecommendParamsDto paramsDto) {
        JwtUserDto jUser = getUserInfo(request);
        paramsDto.setSex(jUser.getSex());
        paramsDto.setUserId(jUser.getUserId());

        clickService.addNovelUserRecommend(paramsDto);
        return RespUtil.success();
    }

    /**
     * @description 用户取消推荐
     * @author sh-sunb6
     * @date 2019-03-09 13:52
     */
    @ApiOperation(value = "用户取消推荐", notes = "需用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "系统token", required = true, paramType = "header"),
            @ApiImplicitParam(name="novelId", value="小说id", required=true, paramType="query", dataType = "int", defaultValue = "")
    })
    @PutMapping("/cancel")
    @UserLoginToken
    public RespResult cancelUserRecommend(HttpServletRequest request, int novelId) {
        JwtUserDto jUser = getUserInfo(request);

        clickService.cancelNovelUserRecommend(novelId, jUser.getUserId());
        return RespUtil.success();
    }

    /**
     * @description 判断用户是否推荐过小说
     * @author sh-sunb6
     * @date 2019-03-09 13:59
     */
    @ApiOperation(value = "用户是否推荐(true推荐过, false未推荐过)", notes = "需用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "系统token", required = true, paramType = "header"),
            @ApiImplicitParam(name="novelId", value="小说id", required=true, paramType="query", dataType = "int", defaultValue = "")
    })
    @GetMapping("/isRecommend")
    @UserLoginToken
    public RespResult isUserRecommend(HttpServletRequest request, int novelId) {
        JwtUserDto jUser = getUserInfo(request);

        return RespUtil.success(clickService.isUserRecommendNovel(novelId, jUser.getUserId()));
    }

    /**
     * @description 系统点击记录
     * @author sh-sunb6
     * @date 2019-03-09 13:51
     */
    @ApiOperation(value = "系统点击记录", notes = "用户点击小说标题进入详情页触发")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "novelId", value = "小说Id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "novelName", value = "小说名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name="novelType", value="小说类型", required=true, paramType="query", dataType = "int", defaultValue = ""),
            @ApiImplicitParam(name="typeChannel", value="小说归属", required=true, paramType="query", dataType = "int", defaultValue = ""),
            @ApiImplicitParam(name="sex", value="性别(有就传,没有不传)", required=false, paramType="query", dataType = "int", defaultValue = "")
    })
    @PostMapping("/system")
    public RespResult addUserRecommend(NovelClickParamsDto paramsDto) {

        clickService.addNovelSystemRecommend(paramsDto);
        return RespUtil.success();
    }

}
